package org.csu.mall.service.Impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.csu.mall.common.CONSTANT;
import org.csu.mall.common.UniformResponse;
import org.csu.mall.entity.PayInfo;
import org.csu.mall.entity.User;
import org.csu.mall.persistence.PayInfoMapper;
import org.csu.mall.persistence.UserMapper;
import org.csu.mall.service.IPayInfoService;
import org.csu.mall.utils.BigDecimalUtil;
import org.csu.mall.utils.UsernameUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.ext.DeclHandler;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;

@Service
public class PayInfoServiceImpl implements IPayInfoService {
    @Autowired
    private PayInfoMapper payInfoMapper;
    @Autowired
    private UserMapper userMapper;

    @Override
    public UniformResponse getStatus() {
        BigInteger orderNo=new BigInteger(UsernameUtil.getOrderNo("orderno"));
        BigDecimal balance=new BigDecimal(UsernameUtil.getBalance("balance"));
        PayInfo payInfo=payInfoMapper.selectOne(Wrappers.<PayInfo>query().eq("order_no",orderNo));
        User user=userMapper.selectById(payInfo.getUserId());
        user.setBalance(BigDecimalUtil.add(user.getBalance().doubleValue(),balance.doubleValue()));
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateById(user);
        UsernameUtil.setBalance("balance",new BigDecimal(0));
        System.out.println(user.getBalance());
        return UniformResponse.createForSuccess("充值成功",payInfo.getPlatformStatus());
    }
}
